<?

/**
 * Action collector. 
 * Some modified POJO class to save info about each action from spotlight.xml configuration file.
 *   
 * @package SpotLight.core.configurator.collector
 * @version since begin
 * @author asmhack
 * @license GPL
 * @copyright Serhiy Zaporozhets'
 */
class ActionColl {
   
   private $interceptors = array ();
   
   /**
    * Action name
    *
    * @var string
    */
   private $name = '';
   
   /**
    * Action class mapping
    *
    * @var string
    */
   private $class = null;
   
   /**
    * file to action class.
    *
    * @var string
    */
   private $file = null;
   
   /**
    * file to action class.
    *
    * @var string
    */
   private $method = 'execute';
   
   /**
    * not required parametr
    * list of result for non ajax action
    *
    * @var string
    */
   private $results = array ();
   
   public function __construct() {
   
   }
   
   public function getMethod() {
      return $this->method;
   }
   
   public function setMethod($method) {
      $this->method = $method;
   }
   
   public function addInterceptor($interceptor) {
      $this->interceptors [] = $interceptor;
   }
   
   public function getInterceptor() {
      return $this->interceptors;
   }
   
   public function setName($name) {
      $this->name = $name;
   }
   
   public function getName() {
      return $this->name;
   }
   
   public function setClass($class) {
      $this->class = $class;
   }
   
   public function getClass() {
      return $this->class;
   }
   
   public function hasClass() {
      return $this->class != null;
   }
   
   private function hasInterceptors() {
      return count ( $this->interceptors ) > 0;
   }
   
   public function hasBeforeInterceptors() {
      if (! $this->hasInterceptors ())
         return false;
      foreach ( $this->interceptors as $interceptor )
         if (strtolower ( $interceptor->getType () ) == 'before')
            return true;
      return false;
   }
   
   public function hasAfterInterceptors() {
      if (! $this->hasInterceptors ())
         return false;
      foreach ( $this->interceptors as $filter )
         if (strtolower ( $filter->getType () ) == 'after')
            return true;
      return false;
   }
   
   public function getAfterInterceptors() {
      return $this->getInterceptorsByType ( 'after' );
   }
   
   public function getBeforeInterceptors() {
      return $this->getInterceptorsByType ( 'before' );
   }
   
   private function getInterceptorsByType($type) {
      $ret = array ();
      foreach ( $this->interceptors as $interceptor )
         if (strtolower ( $interceptor->getType () ) == $type)
            $ret [] = $interceptor;
      return $ret;
   }
   
   public function addResult($result) {
      $this->results [] = $result;
   }
   
   public function getResults() {
      return $this->results;
   }
   
   public function hasResults() {
      return count ( $this->results ) > 0;
   }
   
   public function getResultsAmount() {
      return count ( $this->results );
   }
   
   /**
    * <result *>{return this text if is exist}</result>
    *
    * @param string $resultName return name (success,error,etc...)
    * @return string redirect action URI
    */
   public function getFirstResult() {
      //$resultName=strtolower($resultName);
      foreach ( $this->results as $res ) {
         return $res->getAction ();
      }
      return null;
   }
   
   public function getFirstResultType() {
      //$resultName=strtolower($resultName);
      foreach ( $this->results as $res ) {
         return $res->getType ();
      }
      
      return null;
   }
   
   /**
    * <result *>{return this text if is exist}</result>
    *
    * @param string $resultName return name (success,error,etc...)
    * @return string redirect action URI
    */
   public function getResult($resultName) {
      foreach ( $this->results as $res ) {
         if ($res->getName () == $resultName) {
            return $res->getAction ();
         }
      }
      return null;
   }
   
   public function getResultType($resultName) {
      foreach ( $this->results as $res ) {
         if ($res->getName () == $resultName) {
            return $res->getType ();
         }
      }
      return null;
   }
   
   public function setFile($file) {
      $this->file = $file;
   }
   
   public function getFile() {
      return $this->file;
   }
   
   public function __toString() {
      $txt = '&nbsp;&nbsp;&nbsp;&nbsp;Action <b style="color:blue">' . $this->name . '</b> for <b>' . $this->class . '</b> {<br/>';
      foreach ( $this->results as $res ) {
         $txt .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $res;
      }
      $txt .= '<br/>';
      foreach ( $this->interceptors as $interceptor ) {
         $txt .= '&nbsp;&nbsp;&nbsp;' . $interceptor . '<br/>';
      }
      $txt .= '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}';
      return $txt;
   }

}
?>